/**
 * 
 */
package org.shortestpath.service.dao.util;

import org.shortestpath.service.DbService;
import org.shortestpath.service.dao.DbDao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import org.postgis.Geometry;
import org.postgis.LineString;
import org.postgis.MultiLineString;
import org.postgis.PGgeometry;

/**
 * @author Thilani
 *
 */
public class DbUtility {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		String tableCreate = "CREATE TABLE simpleroads (gid serial NOT NULL, road_id bigint, x1_cor double precision, " +
				"x2_cor double precision, y1_cor double precision, y2_cor double precision, the_geom geometry,  CONSTRAINT " +
				"simplroadstable_pkey PRIMARY KEY (gid))";
		DbDao dbDao = new DbDao();
		try {
			dbDao.createSimpleTable(tableCreate);
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		ResultSet resultSet;
		try {
			resultSet = dbDao.getAllRoads();
			int gid = 0;
			while (resultSet.next()) {
				/*
				 * Retrieve the geometry as an object then cast it to the
				 * geometry type. Print things out.
				 */
				PGgeometry pGgeometry = (PGgeometry) resultSet.getObject(1);
				int road_id = Integer.parseInt(resultSet.getObject(2).toString());
				if( pGgeometry.getGeoType() == Geometry.MULTILINESTRING ) { 
					MultiLineString multiLineString = (MultiLineString)pGgeometry.getGeometry(); 
					LineString[] lineStrings = multiLineString.getLines();
					for (int j =0; j<lineStrings.length; j++){
						LineString lineString = lineStrings[j];
						org.postgis.Point[] roadPoints = lineString.getPoints();
						for (int k =0; k<roadPoints.length -1; k++){
							org.postgis.Point roadPoint = roadPoints[k];
							org.postgis.Point roadPoint2 = roadPoints[k+1];
							
							org.postgis.Point[] twoPoints = new org.postgis.Point[2];
							twoPoints[0]= roadPoint;
							twoPoints[1]= roadPoint2;
							LineString simpleLineString = new LineString(twoPoints);
							LineString[] simpleLineStrings = new LineString[1];
							simpleLineStrings[0]= simpleLineString;
							MultiLineString simpleMultiLineString = new MultiLineString(simpleLineStrings);
							PGgeometry simplePGgeometry = new PGgeometry(simpleMultiLineString);
							
							//String insertTable = "INSERT INTO simpleroads(gid, x_cor, y_cor, the_geom) VALUES ("+gid+", "+roadPoint.getX()+", "+roadPoint.getY()+", "+pGgeometry.getGeometry()+");";
							String insertTable = "INSERT INTO simpleroads(gid, road_id, x1_cor, x2_cor, y1_cor, y2_cor) VALUES ("
								+gid+", "+road_id+", "+roadPoint.getX()+", "+roadPoint2.getX()+ ", "+roadPoint.getY()+", "+roadPoint2.getY()
								+");";
							
							
							
							dbDao.setSimpletableData(insertTable);
							gid++;
						}
					}

				}else {
					//TODO Throw exception
				}
			}
			resultSet.close();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}		
			

	}

}
